package player;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import StateExpress.Collision;

import play.game;
import play.score;
import pointsHandler.point;

import coordinates.Cords;
import coordinates.Fronta;
import coordinates.Prvek;


import tag.Canvas;

import tag.constants;

 
/** "Composite" */
class CompositeSnake implements Cords {

	Fronta fronta =new Fronta();

	Fronta getFronta(){
		return fronta;
	}

    //Adds the graphic to the composition.
    public void add(Cords graphic) {
         
	    fronta.add(new Prvek(graphic)); 
	    
    }
 
    //Removes the graphic from the composition.
    public void removeLast() {
    	fronta.put();
        //mChildGraphics.remove(0);
    }

    public ArrayList<Prvek> get_array_y(){
    	
    	return fronta.getObsah();
    }
    
    public ArrayList<Prvek> get_array_x(){
    	
    	return fronta.getObsah();
    }

    
    
	public float get_x() {
		return 0;
	}

	public float get_y() {
		return 0;
	}

	public boolean isUnique(float onX, float onY) {
        //FIXME slow
		for (Prvek prvek : fronta.getObsah()) {
			
			Cords list = prvek.getHodnota();
			if(list.get_x()==onX && list.get_y()==onY) return false;
			
    	}
		
		return true;
	}

}
 
/** "Leaf" */
class Leaf implements Cords {
float x;
float y;

	Leaf(float x, float y){
		this.x=x;
		this.y=y;
	}

	public float get_x() {
		return x;
	}

	public float get_y() {
		return y;
	}
    

    
}
 

public class snakeObject implements Observer,Canvas,constants{
	
	
	int current = 0;
	
	Collision collision = new Collision();
	CompositeSnake graphic = new CompositeSnake();

	boolean directionChanged = false;
	boolean justCollected = false;
	byte temp_direction = 0;
	public static final byte MOVE_UP = 0;
	public static final byte MOVE_DOWN = 1;
	public static final byte MOVE_LEFT = 2;
	public static final byte MOVE_RIGHT = 3;
	
	public void setDirection(byte dir){
		
		switch(dir){
			case 0 : collision.moveUp(); break;
			case 1 : collision.moveDown(); break;
			case 2 : collision.moveLeft(); break;
			case 3 : collision.moveRight(); break;
		}
	}
	
	
	public snakeObject(){
		graphic.add(new Leaf(0,0));
	}


	public void move(){
		collision.move();
		executeMove();
	}


	public void executeMove() {
		float x =collision.get_x();
		float y =collision.get_y();
		
		if(!graphic.isUnique(x,y) || !collision.check()){
			game.over();
		}
		
		++current;
		if(current>snake_min_size && !justCollected)graphic.removeLast();
		else justCollected=false;
		
		graphic.add(new Leaf(x,y));
		
		if(!graphic.isUnique(point.purple_x,point.purple_y)){
			point.generatePoint();
			score.purple++;
			justCollected=true;
		}
		
	}





    public void update (Observable obj, Object arg)
		  {
		        if (arg instanceof String)
		        {
		//            resp = (String) arg;
		            if(directionChanged){
		            	directionChanged=false;
		            	
						setDirection(temp_direction);
		            }
		        	move();
		            pane.repaint();
		        }
		    }



	public float[][] getCoords() {
		 
			float xpoints[] = new float[graphic.getFronta().getObsah().size()];
		    float ypoints[] = new float[graphic.getFronta().getObsah().size()];
		    int npoints = graphic.getFronta().getObsah().size();
		    
		    
		    //FIXME direct x and y coords
		    //Integer[] _x = graphic.get_array_x().toArray(new Integer[npoints]);
		    //Integer[] _y = graphic.get_array_y().toArray(new Integer[npoints]);
	        
		    
		    
		    int i=0;
		    //FIXME slow
		    for (Prvek x: graphic.get_array_x()) 
		    	xpoints[i++]=x.getHodnota().get_x();
		    
		    i=0;
		    //FIXME slow
		    for (Prvek y: graphic.get_array_y()) 
		    	ypoints[i++]=y.getHodnota().get_y();
		    
		    
		    
		    float[][] tmp = {xpoints,ypoints,{npoints}};
		    return tmp;
		 
		
	}


	public void setDirectionInNextThreadInvoke(byte new_direction) {
		directionChanged=true;
		temp_direction=new_direction;
		
	}

		
	
}